var express = require('express');
var router = express.Router();
let crypto = require('crypto');
const hash = crypto.createHash('md5');
const str = '你好啊 宝贝';
hash.update(str);//
console.log(hash.digest('hex'));
let cryptoMsg;
let users = [
  {
    id: 1,
    name: '小胡',
    length: 20000
  },
  {
    id: 2,
    name: '小吴',
    length: 8000
  },
  {
    id: 20,
    name: '小吴',
    length: 8000
  },
  {
    id: 12,
    name: '小吴',
    length: 8000
  },
];


/* GET home page. */
router.post('/login', function (req, res, next) {
  const hash = crypto.createHash('md5');// sha256: sha512:  sha128 : sha1 (完全不安全)
  const str = '你好啊 宝贝';
  hash.update(str);//
  cryptoMsg = hash.digest('hex')
  res.json({
    code: 1,
    token: cryptoMsg // 请求体 过期时间  key: 用户设置
  });
});

router.get('/list1', function (req, res, next) {
  let {token} = req.query;
  if (token === cryptoMsg) {
    res.json({code: 1, users});
  } else {
    res.json({code: 0, msg: 'token error'});
  }
});

router.get('/item', function (req, res, next) {
  let {id} = req.query;
  id = Number(id);
  let index = users.findIndex(item => item.id === id);
  if (index !== -1) {
    res.json({code: 1, obj: users[index]});
  } else {
    res.json({code: 0, msg: 'error'});

  }
});

module.exports = router;
